<html>
<head><meta charset="utf-8"><title>Misoptimization due to Rust/HW memory model mismatch · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Misoptimization.20due.20to.20Rust.2FHW.20memory.20model.20mismatch.html">Misoptimization due to Rust/HW memory model mismatch</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="169291060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Misoptimization%20due%20to%20Rust/HW%20memory%20model%20mismatch/near/169291060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Misoptimization.20due.20to.20Rust.2FHW.20memory.20model.20mismatch.html#169291060">(Jun 29 2019 at 10:04)</a>:</h4>
<p>Does anyone know an example demonstrating that reasoning based on the hardware concurrency memory model when writing Rust does not work? Basically something like <a href="https://github.com/rust-lang/rust/issues/32976#issuecomment-446775360" target="_blank" title="https://github.com/rust-lang/rust/issues/32976#issuecomment-446775360">this</a> but for concurrency and in Rust. ;)<br>
I've been thinking maybe one could use <code>Relaxed</code> where one should use <code>Release</code>/<code>Acquire</code> -- on x86 that compiles the same, so "what the hardware does" would work, but if we can get LLVM to reorder an access across the <code>Relaxed</code> that it would not be allowed to reorder across the <code>Release</code>/<code>Acquire</code>, that would demonstrate that "what the hardware does" just does not matter.</p>



<a name="169291162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Misoptimization%20due%20to%20Rust/HW%20memory%20model%20mismatch/near/169291162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Misoptimization.20due.20to.20Rust.2FHW.20memory.20model.20mismatch.html#169291162">(Jun 29 2019 at 10:08)</a>:</h4>
<p>Here's a program exhibitng UB this way, but I dont know how to make LLVM optimize it the bad way: <a href="https://play.rust-lang.org/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=11a26c7f5ba7c7777501f3b8040776f6" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=11a26c7f5ba7c7777501f3b8040776f6">https://play.rust-lang.org/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=11a26c7f5ba7c7777501f3b8040776f6</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>